/* ========================================
 *
 * Copyright YOUR COMPANY, THE YEAR
 * All Rights Reserved
 * UNPUBLISHED, LICENSED SOFTWARE.
 *
 * CONFIDENTIAL AND PROPRIETARY INFORMATION
 * WHICH IS THE PROPERTY OF your company.
 *
 * ========================================
*/
#include <device.h>

extern  uint8 toggle_flag;
void main()
{
	 char* content="CSCE 462";  
	int n=0;
	int t=0;
	uint16 diff=0;
	uint16 counter=0;
    uint16 voltageCount;
	uint16 referencecount=0;
    
    ADC_DelSig_1_Start();  /* Configure and power up ADC */
    LCD_Char_1_Start(); /* Initialize and clear the LCD */
    LCD_Char_1_Position(0,0); /* Move the cursor to Row 0 Column 0 */
    
    /* Print Label for the pot voltage raw count */
    LCD_Char_1_PrintString("Voltage : ");
    ADC_DelSig_1_StartConvert(); /* Force ADC to initiate a conversion */

	
	/* Enable global interrupts */
	CYGlobalIntEnable;
	// Initialize, enable interrupt, timer
	isr_1_Start();
	Timer_1_Start();
	

	for(;;)
	{
		/* Wait for end of conversion */
        ADC_DelSig_1_IsEndConversion(ADC_DelSig_1_WAIT_FOR_RESULT); 
        voltageCount = ADC_DelSig_1_GetResult16(); /* Get converted result */

        /* Set range limit */
        if (voltageCount > 0x03FF)
        {
           voltageCount = 0x03FF;
        }
        else 
        {
            /* Continue on */
        }
        LCD_Char_1_Position(0,9);
        LCD_Char_1_PrintInt16(diff); /* Print result on LCD */

		if(voltageCount>0x2A3)
			{
				if(t>0)
				{
				t=0;
				n=n+1;
				diff=counter-referencecount;
				referencecount=counter;
				}
			}
				
		if(voltageCount<0x2A3)
			{
				t=1;
			}
		if(toggle_flag == 1)
		{
			LED1_Write(~LED1_Read());
			toggle_flag = 0;
			counter = counter+1;
		}
	}
}